package com.androidservice.demo.web;

import com.androidservice.demo.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("user")
@Api(description = "用户信息操作API")
public class UserController {

    @Autowired
    private UserService userService;


    @PostMapping("login")
    @ApiOperation(value = "获取指定用户数据" , notes = "根据用户id获取用户数据")
    @ApiImplicitParams({@ApiImplicitParam(name = "mEmail", value = "用户名", required = true, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "mPassword", value = "密码", required = true, dataType = "String", paramType = "form")
    })
    public ResponseEntity<Map<String,Object>> doLogin(@RequestParam("mEmail") String mEmail,
                                                      @RequestParam("mPassword") String mPassword){
        Map<String,Object> resultmap = new HashMap<>();
        try {

            System.err.print("登录开始....");

            boolean flag = this.userService.doLogin(mEmail,mPassword);
            if(flag){
                resultmap.put("code",flag);
                resultmap.put("message","登录成功");
                System.err.print("登录结束...."+flag);
                return ResponseEntity.ok(resultmap);
            }
            resultmap.put("code",flag);
            resultmap.put("message","登录失败");
            System.err.print("登录结束...."+flag);
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(resultmap);
        } catch (Exception e){
            e.printStackTrace();
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(resultmap);
    }


}
